package com.android.a8pool;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class Billard extends Activity {
    BillardWorld mWorld;
    private Handler mHandler;
    Thread worldThread;
	boolean onRun=true;
	boolean onMove = false;

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Jeu billard");

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       
       
          DisplayMetrics dm = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(dm);
          
        mWorld = new BillardWorld(this,dm.widthPixels,dm.heightPixels);
        this.setContentView(this.mWorld);

        // Add 10 Balls
        for (int i=0; i<16; i++) {
            mWorld.addBallRand();
        }

        // Start Regular Update
        worldThread = new Thread(update);
    }
   
   
   
    @Override
    protected void onPause() {
        super.onPause();
        worldThread.interrupt();
    }

    private Runnable update = new Runnable() {
    	boolean onRun=true;
        public void run() {
            while (onRun)
            	mWorld.update();
            //mHandler.postDelayed(update, (long) (mWorld.timeStep*1000));
        }
    };
   
    @Override public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
    	int N = event.getHistorySize();

    	if (action == MotionEvent.ACTION_DOWN|| action == MotionEvent.ACTION_MOVE){
    		mWorld.impulse.x = event.getX();
    		mWorld.impulse.y = mWorld.World_H-event.getY();
    		mWorld.drawImpulse = true;
    		mWorld.postInvalidate();
        }
        if (action == MotionEvent.ACTION_UP)
        {
        		mWorld.drawImpulse = false;
        		mWorld.impulse1(event.getX(), mWorld.World_H-event.getY());
        }
        return true;
    }

   
    @Override
    protected void onResume()
    {
         super.onResume();
         worldThread.start();
    }
   
    @Override
    protected void onStop()
    {
         super.onStop();
         worldThread.interrupt();
    }
}
